package XZK010404.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import XZK010404.entity.AirInfo;

/**
 * 航班信息 DAO 实现
 *
 * @author bing.zhang06@hand-china.com 2021/7/7 21:34
 **/
public class AirInfoDaoImpl implements AirInfoDao {
    private BaseDao baseDao;

    public void setBaseDao(BaseDao baseDao) {
        this.baseDao = baseDao;
    }

    @Override
    public List<AirInfo> listAll() {
        Connection connection = BaseDao.getConnection();
        try (PreparedStatement pst = connection.prepareStatement("select * from airinfo")) {
            ResultSet rs = pst.executeQuery();
            List<AirInfo> infoList = new ArrayList<>();
            while (rs.next()) {
                AirInfo airInfo = new AirInfo();
                airInfo.setId(rs.getInt("id"));
                airInfo.setAirNo(rs.getString("air_no"));
                airInfo.setDestination(rs.getString("destination"));
                airInfo.setDepartureDate(rs.getDate("departure_date"));
                infoList.add(airInfo);
            }
            return infoList;
        } catch (SQLException e) {
            return null;
        }
    }

    @Override
    public List<AirInfo> getByDepartureDate(String departureDate) {
        Connection connection = BaseDao.getConnection();
        try (PreparedStatement pst = connection.prepareStatement("select * from airinfo where departure_date = ?")) {
            pst.setString(1, departureDate);
            ResultSet rs = pst.executeQuery();
            List<AirInfo> infoList = new ArrayList<>();
            while (rs.next()) {
                AirInfo airInfo = new AirInfo();
                airInfo.setId(rs.getInt("id"));
                airInfo.setAirNo(rs.getString("air_no"));
                airInfo.setDestination(rs.getString("destination"));
                airInfo.setDepartureDate(rs.getDate("departure_date"));
                infoList.add(airInfo);
            }
            return infoList;
        } catch (SQLException e) {
            return null;
        }
    }

    @Override
    public List<AirInfo> getByDestination(String destination) {
        Connection connection = BaseDao.getConnection();
        try (PreparedStatement pst = connection.prepareStatement("select * from airinfo where destination = ?")) {
            pst.setString(1, destination);
            ResultSet rs = pst.executeQuery();
            List<AirInfo> infoList = new ArrayList<>();
            while (rs.next()) {
                AirInfo airInfo = new AirInfo();
                airInfo.setId(rs.getInt("id"));
                airInfo.setAirNo(rs.getString("air_no"));
                airInfo.setDestination(rs.getString("destination"));
                airInfo.setDepartureDate(rs.getDate("departure_date"));
                infoList.add(airInfo);
            }
            return infoList;
        } catch (SQLException e) {
            return null;
        }
    }

    @Override
    public int deleteByAirNo(String airNo) {
        Connection connection = BaseDao.getConnection();
        try (PreparedStatement pst = connection.prepareStatement("delete from airinfo where air_no = ?")) {
            pst.setString(1, airNo);
            int rows = pst.executeUpdate();
            return rows;
        } catch (SQLException e) {
            return 0;
        }
    }

    @Override
    public int update(AirInfo airInfo) {
        Connection connection = BaseDao.getConnection();
        try (PreparedStatement pst = connection.prepareStatement("update airinfo(air_no,destination,departure_date) values(?,?,?) where id = ?")) {
            pst.setString(1, airInfo.getAirNo());
            pst.setString(2, airInfo.getDestination());
            pst.setDate(3, (java.sql.Date) airInfo.getDepartureDate());
            pst.setInt(4, airInfo.getId());
            int rows = pst.executeUpdate();
            return rows;
        } catch (SQLException e) {
            return 0;
        }
    }
}
